﻿
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebTeamplate
{
    /// <summary>
    /// Gives default button to a control during the loading of the page.
    /// </summary>
    public class ClientDefaultButton
    {
        public static void SetDefaultButton(System.Web.UI.Page page, System.Web.UI.HtmlControls.HtmlInputButton button)
        {
            SetDefaultButton(page, button.ClientID);
        }
        public static void SetDefaultButton(System.Web.UI.Page page, System.Web.UI.WebControls.Button button)
        {
            SetDefaultButton(page, button.ClientID);
        }
        public static void SetDefaultButton(System.Web.UI.Page page, System.Web.UI.WebControls.ImageButton button)
        {
            SetDefaultButton(page, button.ClientID);
        }
        public static void SetDefaultButton(System.Web.UI.Page page, System.Web.UI.HtmlControls.HtmlInputImage image)
        {
            SetDefaultButton(page, image.ClientID);
        }
        public static void SetDefaultButton(System.Web.UI.Page page, System.Web.UI.WebControls.ImageButton button, bool disableTextbox)
        {
            SetDefaultButton(page, button.ClientID, disableTextbox);
        }
        public static void SetDefaultButton(System.Web.UI.Page page, string clientID, bool disableTex)
        {
            string txt = "";
            if (disableTex)
                txt = "&& activeInputType != 'text'";
            page.RegisterStartupScript("DefaultButton", "<script>\r\n\t" +
              "window.document.onkeydown = KeyDownHandler;\r\n\t" +
              "function KeyDownHandler() {\r\n\t\t" +
              "activeInputType = document.activeElement.type;\r\n\t\t var defaultButton = document.all['" + clientID + "'];\r\n\t\t" +
              "if (event.keyCode == 13 && activeInputType != 'button' && activeInputType != 'image' && activeInputType != 'textarea' " + txt + ") {\r\n\t\t\t" +
                      "activeInputTag = document.activeElement.tagName;\r\n\t\t\t		if( activeInputTag != 'A'){\r\n\t\t\t			" +
              "event.returnValue = false; \r\n\t\t\t" +
              "event.cancel = true;\r\n\t\t\t" +
                      "defaultButton.click();\r\n\t\t}\r\n\t\t}\r\n\t}\r\n\t</script>");
        }
        public static void SetDefaultButton(System.Web.UI.Page page, string clientID, params string[] whithoutTextboxIds)
        {
            string various = "";
            string condition = "";

            foreach (string s in whithoutTextboxIds)
            {
                various += "var " + s + " = document.all['" + s + "'];\r\n\t\t";
                condition += " && activeInputId != " + s + ".id";
            }
            page.RegisterStartupScript("DefaultButton", "<script>\r\n\t" +
              "window.document.onkeydown = KeyDownHandler;\r\n\t" +
              "function KeyDownHandler() {\r\n\t\t" +
              "activeInputType = document.activeElement.type;\r\n\t\tactiveInputId = document.activeElement.id;\r\n\t\t var defaultButton = document.all['" + clientID + "'];" + various +

              "if (event.keyCode == 13 && activeInputType != 'button' && activeInputType != 'image' && activeInputType != 'textarea'" + condition + ") {\r\n\t\t\t" +
              "event.returnValue = false;\r\n\t\t\t" +
              "event.cancel = true;\r\n\t\t\t" +
              "defaultButton.click();\r\n\t\t}\r\n\t}\r\n\t</script>");
        }
        public static void SetDefaultButton1(System.Web.UI.Page page, string clientID1, string clientID2, params string[] whithoutTextboxIds)
        {
            string various = "";
            string condition = "";
            string condition1 = "";

            foreach (string s in whithoutTextboxIds)
            {
                various += "var " + s + " = document.all['" + s + "'];\r\n\t\t";
                condition += " && activeInputId != " + s + ".id";
            }
            foreach (string s in whithoutTextboxIds)
            {
                various += "var " + s + " = document.all['" + s + "'];\r\n\t\t";
                condition1 += "|| activeInputId == " + s + ".id ";
            }
            condition1 = condition1.Remove(0, 2);
            page.RegisterStartupScript("DefaultButton", "<script>\r\n\t" +
              "window.document.onkeydown = KeyDownHandler;\r\n\t" +
              "function KeyDownHandler() {\r\n\t\t" +
              "activeInputType = document.activeElement.type;\r\n\t\tactiveInputId = document.activeElement.id;\r\n\t\t var defaultButton = document.all['" + clientID1 + "']; var defaultButton1 = document.all['" + clientID2 + "'];" + various +
              "if ((activeInputType == null) || (event.keyCode == 13 && activeInputType != 'button' && activeInputType != 'image' && activeInputType != 'textarea' " + condition + ")) {\r\n\t\t\t" +
              "event.returnValue = false;\r\n\t\t\t" +
              "event.cancel = true;\r\n\t\t\t" +
              " if( (activeInputType == null) || (event.keyCode == 13 && activeInputType != 'button' && activeInputType != 'image' && activeInputType != 'textarea' && (" + condition1 + "))) {\r\n\t\t\t" +
              " if( event.keyCode == 13 )defaultButton.click();\r\n\t\t}\r\n\t else {  if( event.keyCode == 13 )defaultButton1.click();\r\n\t\t}\r\n\t}\r\n\t}</script>");
        }
        public static void SetDefaultButton(System.Web.UI.Page page, string clientID)
        {
            page.RegisterStartupScript("DefaultButton", "<script>\r\n\t" +
            "	window.document.onkeydown = function(evt)\r\n\t	{ \r\n\t\t" +
            "var et = (window.event)?event:evt;  \r\n\t\t" +
          "var target;	\r\n\t\t" +
          "if (\"activeElement\" in document){\r\n\t\t" +
          "\r\n\t\t// Internet Explorer and Opera\r\n\t\t" +
          "target = document.activeElement; \r\n\t\t" +
          "}\r\n\t\t" +
          "else {\r\n\t\t" +
          "		// Firefox\r\n\t\t" +
          "\r\n\ttarget = et ? et.explicitOriginalTarget : null;" +
          "}\r\n\t\t" +
          "var defaultButton = document.getElementById('" + clientID + "');\r\n\t\t" +
          "if (et.keyCode == 13 && !(target && (target.tagName.toLowerCase() == \"textarea\"))&& target.tagName.toLowerCase() != 'a' && target.type != 'image' ) \r\n\t\t" +
          "{ \r\n\t\t " +
           " if( checkbrowser() == 'isIE'){ \r\n\t\t" +
             " if(document.readyState != 'complete'){\r\n\t\t" +
               " return false;\r\n\t\t" +
              "}\r\n\t\t" +
              "et.returnValue = false;\r\n\t\t" +
              "et.cancel = true;  \r\n\t\t  " +
            "}\r\n\t\t" +
          "if(target.type =='submit'){ \r\n\t\t " +
            "if(target.id == defaultButton.id ){     \r\n\t\t    " +
              "if( Page_ClientValidate()) {\r\n\t\t" +
                "if( checkbrowser() == 'isIE'){\r\n\t\t " +
                  "defaultButton.click();\r\n\t\t " +
                "}\r\n\t\t " +
                "else if( checkbrowser() == 'isFF'){ \r\n\t\t" +
                  "var e = document.createEvent(\"MouseEvents\");\r\n\t\t" +
                  "e.initEvent(\"click\", true, true);\r\n\t\t" +
               "   defaultButton.dispatchEvent;\r\n\t\t" +
              "  }\r\n\t\t" +
             " }\r\n\t\t" +
            "}\r\n\t\t" +
            "else {        \r\n\t\t " +
            "  if(checkbrowser() == 'isIE')						\r\n\t\t " +
             " {																		\r\n\t\t " +
            "   target.click();								\r\n\t\t " +
            "  }																 \r\n\t\t" +
            "  else if( checkbrowser() == 'isFF')\r\n\t\t" +
            "  {																						\r\n\t\t			" +
            "    var e = document.createEvent(\"MouseEvents\");	\r\n\t\t	" +
            "    e.initEvent(\"click\", true, true);  \r\n\t\t          " +
            "    target.dispatchEvent;\r\n\t\t" +
            "  }\r\n\t\t" +
           " }\r\n\t\t" +
          "}\r\n\t\t" +
         " else    {\r\n\t\t" +
            "if(Page_ClientValidate())      {\r\n\t\t" +
              "if(checkbrowser() == 'isIE')\r\n\t\t" +
              "{\r\n\t\t" +
               " defaultButton.click();\r\n\t\t" +
              "}\r\n\t\t" +
              "else if( checkbrowser() == 'isFF'){ \r\n\t\t" +
               " var e = document.createEvent(\"MouseEvents\");		\r\n\t\t" +
               " e.initEvent(\"click\", true, true);            \r\n\t\t" +
               " defaultButton.dispatchEvent;\r\n\t\t" +
              "} \r\n\t\t" +
            "}\r\n\t\t" +
          "}\r\n\t\t" +
        "}\r\n\t\t" +
      "};</script>");
        }
        public static void SetPageDefaultButton(System.Web.UI.Page page, string buttonID)
        {

            string script = @" <script language='javascript' type='text/javascript'>



      window.document.onkeydown = function(evt) {
          var et = (window.event) ? event : evt;
          var target;
          if ('activeElement' in document) {
              // Internet Explorer and Opera
              target = document.activeElement;
          }
          else {
              // Firefox
              target = et ? et.explicitOriginalTarget : null;
          }
  
          var defaultButton = document.getElementById('" + buttonID + @"');
//debugger;
          if (et.keyCode == 13 && target.tagName.toLowerCase() == 'input') {
         
             // alert(target.type);
              if (target.type == 'text' || target.type == 'password') {
                 
                  defaultButton.focus();
                  if (checkbrowser() == 'isIE') {
                      //target.click();
                        //defaultButton.click();


                  }
                  else if (checkbrowser() == 'isFF') {
                      var e = document.createEvent('MouseEvents');
                      e.initEvent('click', true, true);
                      target.dispatchEvent;
                  }
              }
              else if (target.type == 'submit')
              {
                  if (checkbrowser() == 'isIE')
                  {
                    //  target.click();
                  }
                  else if (checkbrowser() == 'isFF') {
                      var e = document.createEvent('MouseEvents');
                      e.initEvent('click', true, true);
                      target.dispatchEvent;
                  }
              }
              else if (target.type == 'button') 
              {
              if (checkbrowser() == 'isIE') {
                     // target.click();
                  }
                  else if (checkbrowser() == 'isFF') {
                      return;
                  }
              }
              else {
                  try {
                      if (checkbrowser() == 'isIE') {
                          target.click();
                      }
                      else if (checkbrowser() == 'isFF') {
                          var e = document.createEvent('MouseEvents');
                          e.initEvent('click', true, true);
                          target.dispatchEvent;
                      }
                  }
                  catch (e) {
                      return;
                  }
              }
          }
      }
</script>";
            page.RegisterStartupScript("DefaultButton", script);

        }
        public static void SetDefaultButtonSearch(System.Web.UI.Page page, string clientID)
        {
            // SetPageDefaultButton(page, clientID);

            page.RegisterStartupScript("DefaultButton", "<script  language=\"javascript\" type=\"text/javascript\">\r\n\t" +
            "	window.document.onkeydown = function(evt)\r\n\t	{ \r\n\t\t" +
            "var et = (window.event)?event:evt;  \r\n\t\t" +
        "var target;	\r\n\t\t" +
        "if (\"activeElement\" in document){\r\n\t\t" +
        "\r\n\t\t// Internet Explorer and Opera\r\n\t\t" +
        "target = document.activeElement; \r\n\t\t" +
        "}\r\n\t\t" +
        "else {\r\n\t\t" +
        "		// Firefox\r\n\t\t" +
        "\r\n\ttarget = et ? et.explicitOriginalTarget : null;" +
        "}\r\n\t\t" +
        "var defaultButton = document.getElementById('" + clientID + "');\r\n\t\t" +
        "if (et.keyCode == 13 && !(target && (target.tagName.toLowerCase() == \"textarea\"))&& target.tagName.toLowerCase() != 'a' && target.type != 'image' ) \r\n\t\t" +
        "{ \r\n\t\t " +
         " if( checkbrowser() == 'isIE'){ \r\n\t\t" +
             " if(document.readyState != 'complete'){\r\n\t\t" +
                 " return false;\r\n\t\t" +
                "}\r\n\t\t" +
                "et.returnValue = false;\r\n\t\t" +
                "et.cancel = true;  \r\n\t\t  " +
            "}\r\n\t\t" +
        "if(target.type =='submit'){ \r\n\t\t " +
            "if(target.id == defaultButton.id ){     \r\n\t\t    " +
                //"if( Page_ClientValidate()) {\r\n\t\t" +
                    "if( checkbrowser() == 'isIE'){\r\n\t\t " +
                        "defaultButton.click();\r\n\t\t " +
                    "}\r\n\t\t " +
                    "else if( checkbrowser() == 'isFF'){ \r\n\t\t" +
                        "var e = document.createEvent(\"MouseEvents\");\r\n\t\t" +
                        "e.initEvent(\"click\", true, true);\r\n\t\t" +
                 "   defaultButton.dispatchEvent;\r\n\t\t" +
                "  }\r\n\t\t" +
                //" }\r\n\t\t" +
            "}\r\n\t\t" +
            "else {        \r\n\t\t " +
            "  if(checkbrowser() == 'isIE')						\r\n\t\t " +
             " {																		\r\n\t\t " +
            "   target.click();								\r\n\t\t " +
            "  }																 \r\n\t\t" +
            "  else if( checkbrowser() == 'isFF')\r\n\t\t" +
            "  {																						\r\n\t\t			" +
            "    var e = document.createEvent(\"MouseEvents\");	\r\n\t\t	" +
            "    e.initEvent(\"click\", true, true);  \r\n\t\t          " +
            "    target.dispatchEvent;\r\n\t\t" +
            "  }\r\n\t\t" +
         " }\r\n\t\t" +
        "}\r\n\t\t" +
     " else    {\r\n\t\t" +
                //"if(Page_ClientValidate())      {\r\n\t\t" +
                "if(checkbrowser() == 'isIE')\r\n\t\t" +
                "{\r\n\t\t" +
                 " defaultButton.click();\r\n\t\t" +
                "}\r\n\t\t" +
                "else if( checkbrowser() == 'isFF'){ \r\n\t\t" +
                 " var e = document.createEvent(\"MouseEvents\");		\r\n\t\t" +
                 " e.initEvent(\"click\", true, true);            \r\n\t\t" +
                 " defaultButton.dispatchEvent;\r\n\t\t" +
                "} \r\n\t\t" +
                //"}\r\n\t\t" +
        "}\r\n\t\t" +
    "}\r\n\t\t" +
"};</script>");
        }

        public static void SetDefaultButtonSearch(System.Web.UI.Page page, string clientID, int i)
        {
            page.Form.DefaultButton = clientID;
            string script = @"<script>\r\n\tif (typeof (WebForm_FireDefaultButton) != 'undefined')
    var origFireDefaultButton = WebForm_FireDefaultButton;
WebForm_FireDefaultButton = function(event, target) {
  
    if (event.keyCode == 13) {

        var src = event.srcElement || event.target;

        // Don't call original function if focus is on a button
        if (src && (src.tagName.toLowerCase() == 'input') && (src.type.toLowerCase() == 'submit' || src.type.toLowerCase() == 'button')) {
            return true;
        }
    }
    return origFireDefaultButton(event, target);
}</script>";


            page.RegisterStartupScript("DefaultButton", script);
        }
    }// end class
}// end namespace